﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Data;
using System.Text;

namespace Easy.Common.Helper
{
    /// <summary>
    /// JsonHelper
    /// </summary>
    public partial class JsonHelper
    {
        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public static byte[] Serialize(object item)
        {
            var jsonString = JsonConvert.SerializeObject(item);

            return Encoding.UTF8.GetBytes(jsonString);
        }
        /// <summary>
        /// 反序列化
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="value"></param>
        /// <returns></returns>
        public static TEntity Deserialize<TEntity>(byte[] value)
        {
            if (value == null)
            {
                return default;
            }
            var jsonString = Encoding.UTF8.GetString(value);
            return JsonConvert.DeserializeObject<TEntity>(jsonString);
        }

        /// <summary>
        /// Object转Json
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ObjectToJson(object obj)
        {
            return JsonConvert.SerializeObject(obj, new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" });
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="converters"></param>
        /// <returns></returns>
        public static string ObjectToJsonByConverter(object obj, params Newtonsoft.Json.JsonConverter[] converters)
        {
            return JsonConvert.SerializeObject(obj, converters);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <returns></returns>
        public static T JsonToObject<T>(string input)
        {
            return JsonConvert.DeserializeObject<T>(input);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <param name="converter"></param>
        /// <returns></returns>
        public static T JsonToObjectByConverter<T>(string input, params JsonConverter[] converter)
        {
            return JsonConvert.DeserializeObject<T>(input, converter);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="input"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public static T JsonToObjectBySetting<T>(string input, JsonSerializerSettings settings)
        {
            return JsonConvert.DeserializeObject<T>(input, settings);
        }

        /// <summary>
        /// 将对象转换为byte数组
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns>返回结果</returns>
        public static byte[] ObjectToByte(object obj)
        {
            byte[] serializeResult = Encoding.UTF8.GetBytes(ObjectToJson(obj));
            return serializeResult;
        }
        /// <summary>
        /// 将byte数组转换为对象
        /// </summary>
        /// <param name="bytes">byte数组</param>
        /// <returns>返回结果</returns>
        public static object ByteToObject(byte[] bytes)
        {
            return JsonToObject<object>(Encoding.UTF8.GetString(bytes));
        }
        /// <summary>
        /// 将byte数组转为T
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="bytes">byte数组</param>
        /// <returns>返回结果</returns>
        public static T ByteToObject<T>(byte[] bytes)
        {
            return JsonToObject<T>(Encoding.UTF8.GetString(bytes));
        }
        /// <summary>
        /// Null转Empty
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        private static object NullToEmpty(object obj)
        {
            return null;
        }

        /// <summary> 
        /// 数据表转键值对集合
        /// 把DataTable转成 List集合, 存每一行 
        /// 集合中放的是键值对字典,存每一列 
        /// </summary> 
        /// <param name="dt">数据表</param> 
        /// <returns>哈希表数组</returns> 
        public static List<Dictionary<string, object>> DataTableToList(DataTable dt)
        {
            List<Dictionary<string, object>> list
                 = new List<Dictionary<string, object>>();

            foreach (DataRow dr in dt.Rows)
            {
                Dictionary<string, object> dic = new Dictionary<string, object>();
                foreach (DataColumn dc in dt.Columns)
                {
                    dic.Add(dc.ColumnName, dr[dc.ColumnName]);
                }
                list.Add(dic);
            }
            return list;
        }

        /// <summary> 
        /// 数据集转键值对数组字典 
        /// </summary> 
        /// <param name="dataSet">数据集</param> 
        /// <returns>键值对数组字典</returns> 
        public static Dictionary<string, List<Dictionary<string, object>>> DataSetToDic(DataSet ds)
        {
            Dictionary<string, List<Dictionary<string, object>>> result = new Dictionary<string, List<Dictionary<string, object>>>();

            foreach (DataTable dt in ds.Tables)
                result.Add(dt.TableName, DataTableToList(dt));

            return result;
        }

        /// <summary> 
        /// 数据表转Json 
        /// </summary> 
        /// <param name="dataTable">数据表</param> 
        /// <returns>Json字符串</returns> 
        public static string DataTableToJson(DataTable dt)
        {
            return ObjectToJson(DataTableToList(dt));
        }

        /// <summary> 
        /// 将Json文本转换为数据表数据 
        /// </summary> 
        /// <param name="JsonText">Json文本</param> 
        /// <returns>数据表字典</returns> 
        public static Dictionary<string, List<Dictionary<string, object>>> TablesDataFromJson(string JsonText)
        {
            return JsonToObject<Dictionary<string, List<Dictionary<string, object>>>>(JsonText);
        }

        /// <summary> 
        /// 将Json文本转换成数据行 
        /// </summary> 
        /// <param name="JsonText">Json文本</param> 
        /// <returns>数据行的字典</returns>
        public static Dictionary<string, object> DataRowFromJson(string JsonText)
        {
            return JsonToObject<Dictionary<string, object>>(JsonText);
        }
    }
}
